package com.zmkm.controller.product_use;

import com.zmkm.common.Const;
import com.zmkm.common.ServerResponse;
import com.zmkm.entity.Housekeeper;
import com.zmkm.service.IProductService;
import com.zmkm.service.IUserService;
import com.zmkm.vo.ProductRegister;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/product_use_register/")
public class ProductUseRegister {
	@Autowired
	private IProductService iProductService;
	@Autowired
	private IUserService iUserService;

	@RequestMapping(value = "register", produces = { "application/json;charset=utf-8" })
	public ModelAndView register(HttpServletRequest request) {
		ServerResponse response = (ServerResponse) GradeVerify(request, Const.SERVER).getModel().get("serverResponse");
		if (response.getStatus() != 0) {
			return GradeVerify(request, Const.SERVER);
		}
		ModelAndView mv = new ModelAndView();
		ServerResponse responseNew = this.iProductService.ProductUseRegister(prInsert(request));
		mv.addObject("submitReturn", responseNew);
		mv.addObject("returnWeb", "admin/product_use");
		mv.setViewName("admin_product/submitReturn");
		return mv;
	}

	private ProductRegister prInsert(HttpServletRequest request) {
		ProductRegister pr = new ProductRegister();
		String time = request.getParameter("time").trim();
		String product_name = request.getParameter("product_name").trim();
		String product_model = request.getParameter("product_model").trim();
		String wristband_name = request.getParameter("wristband_name").trim();
		String wristband_model = request.getParameter("wristband_model").trim();
		int wristband_number = Integer.parseInt(request.getParameter("wristband_number").trim());
		String client_name = request.getParameter("client_name").trim();
		String client_phone = request.getParameter("client_phone").trim();
		String uptown_name = request.getParameter("uptown_name").trim();
		String client_address = request.getParameter("client_address").trim();
		String Vip_number = request.getParameter("Vip_number").trim();
		String install_man = request.getParameter("install_man").trim();
		String payment = request.getParameter("payment").trim();
		String service_man = request.getParameter("service_man").trim();
		String service_stronghold_name = request.getParameter("service_stronghold_name").trim();
		String remarks = request.getParameter("remarks");
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			pr.setAllProductRegister(df.parse(time), product_name, product_model, wristband_name, wristband_model,
					wristband_number, client_name, client_phone, uptown_name, client_address, Vip_number, install_man,
					payment, service_man, service_stronghold_name, remarks);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return pr;
	}

	@ResponseBody
	@RequestMapping(value ="product_name_select", produces = { "application/json;charset=utf-8" })
	public ServerResponse productNameSelect() {
		List<String> list = this.iProductService.productNameByType(0);
		StringBuffer view = new StringBuffer();
		for (String l : list) {
			view.append("<option>");
			view.append(l);
			view.append("</option>");
		}
		return ServerResponse.createBySuccessMessage(view.toString());
	}

	@ResponseBody
	@RequestMapping(value ="product_model_select", produces = { "application/json;charset=utf-8" })
	public ServerResponse productModelSelect(HttpServletRequest request) {
		String productName = request.getParameter("productName");
		List<String> list = this.iProductService.productModel(productName);
		StringBuffer view = new StringBuffer();
		for (String l : list) {
			view.append("<option>");
			view.append(l);
			view.append("</option>");
		}
		return ServerResponse.createBySuccessMessage(view.toString());
	}

	@ResponseBody
	@RequestMapping(value = "wristband_name_select", produces = { "application/json;charset=utf-8" })
	public ServerResponse wristbandNameSelect() {
		List<String> list = this.iProductService.productNameByType(1);
		StringBuffer view = new StringBuffer();
		for (String l : list) {
			view.append("<option>");
			view.append(l);
			view.append("</option>");
		}
		return ServerResponse.createBySuccessMessage(view.toString());
	}

	public ModelAndView GradeVerify(HttpServletRequest request, ArrayList<Integer> grade) {
		ModelAndView mv = new ModelAndView();
		Housekeeper data = (Housekeeper) request.getSession().getAttribute("housekeeper");
		if (data == null) {
			mv.addObject("serverResponse", ServerResponse.createByErrorMessage("未登陆，请登陆后在进行操作！"));
			mv.setViewName("login");
			return mv;
		}
		if (data != null) {
			ServerResponse verify = this.iUserService.userNameAndpassWordVerify(data.getUserName(), data.getPassWord());
			if (verify.getStatus() == 1) {
				mv.addObject("serverResponse", verify);
				mv.setViewName("login");
				return mv;
			}
			if (!(grade.contains(data.getUser_grade()) || Const.ADMAX.contains(data.getUser_grade()))) {
				mv.addObject("serverResponse", ServerResponse.createByErrorMessage("没有这个操作权限!"));
				request.getSession().setAttribute("housekeeper", data);
				mv.setViewName("index");
				return mv;
			}
		}
		mv.addObject("serverResponse", ServerResponse.createBySuccess());
		return mv;
	}
}

/*
 * Location: C:\Users\Administrator\Desktop\web\zmkm_oa\WEB-INF\classes\
 * Qualified Name: com.zmkm.controller.product_use.ProductUseRegister JD-Core
 * Version: 0.7.0.1
 */